namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceEntertainmentPageEdit : Page
    {
        protected TextBox EntertainmentAddressTxb;
        protected DropDownList EntertainmentCityDdl;
        protected TextBox EntertainmentNameTxb;
        protected TextBox EntertainmentPhoneTxb;
        protected DropDownList EntertainmentProvinceDdl;
        protected DropDownList EntertainmentRecommendationRateDdl;
        protected TextBox EntertainmentTypeTxb;
        protected Image iResource;
        protected Label lbEntertainmentAddress;
        protected Label lbEntertainmentCity;
        protected Label lbEntertainmentName;
        protected Label lbEntertainmentPhone;
        protected Label lbEntertainmentProvince;
        protected Label lbEntertainmentRecommendationRate;
        protected Label lbEntertainmentType;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;

        private void EntertainmentProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.EntertainmentCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.EntertainmentProvinceDdl.SelectedItem.Value));
            this.EntertainmentCityDdl.DataTextField = "regionName";
            this.EntertainmentCityDdl.DataValueField = "regionId";
            this.EntertainmentCityDdl.DataBind();
        }

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["EntertainmentId"]);
                ResourceEntertainment resourceEntertainment = ManageResource.GetResourceEntertainmentInfoViaId(id);
                this.EntertainmentNameTxb.Text = resourceEntertainment.EntertainmentName;
                this.EntertainmentAddressTxb.Text = resourceEntertainment.EntertainmentAddress;
                this.EntertainmentPhoneTxb.Text = resourceEntertainment.EntertainmentPhone;
                this.EntertainmentTypeTxb.Text = resourceEntertainment.EntertainmentType;
                for (int i = 0; i < this.EntertainmentProvinceDdl.Items.Count; i++)
                {
                    if (this.EntertainmentProvinceDdl.Items[i].Value == resourceEntertainment.EntertainmentProvince.ToString())
                    {
                        this.EntertainmentProvinceDdl.SelectedIndex = i;
                        break;
                    }
                }
                this.EntertainmentCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceEntertainment.EntertainmentProvince);
                this.EntertainmentCityDdl.DataTextField = "regionName";
                this.EntertainmentCityDdl.DataValueField = "regionId";
                this.EntertainmentCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.EntertainmentCityDdl.Items.Count; i++)
                {
                    if (this.EntertainmentCityDdl.Items[i].Value == resourceEntertainment.EntertainmentCity.ToString())
                    {
                        this.EntertainmentCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.EntertainmentCityDdl.Items.Clear();
                }
                for (int i = 0; i < this.EntertainmentCityDdl.Items.Count; i++)
                {
                    if (this.EntertainmentCityDdl.Items[i].Value == resourceEntertainment.EntertainmentCity.ToString())
                    {
                        this.EntertainmentCityDdl.SelectedIndex = i;
                    }
                }
                for (int i = 0; i < this.EntertainmentRecommendationRateDdl.Items.Count; i++)
                {
                    if (this.EntertainmentRecommendationRateDdl.Items[i].Value == resourceEntertainment.EntertainmentRecommendationRate)
                    {
                        this.EntertainmentRecommendationRateDdl.SelectedIndex = i;
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            this.EntertainmentProvinceDdl.SelectedIndexChanged += new EventHandler(this.EntertainmentProvinceDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.EntertainmentProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.EntertainmentProvinceDdl.DataTextField = "RegionName";
            this.EntertainmentProvinceDdl.DataValueField = "RegionId";
            this.EntertainmentProvinceDdl.DataBind();
            this.EntertainmentCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.EntertainmentProvinceDdl.SelectedItem.Value));
            this.EntertainmentCityDdl.DataTextField = "regionName";
            this.EntertainmentCityDdl.DataValueField = "regionId";
            this.EntertainmentCityDdl.DataBind();
            string stri = "";
            for (int i = 1; i <= 5; i++)
            {
                ListItem item = new ListItem();
                item.Value = i.ToString();
                stri = stri + "*";
                item.Text = stri;
                this.EntertainmentRecommendationRateDdl.Items.Add(item);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strEntertainmentName = this.EntertainmentNameTxb.Text.Trim();
            string strEntertainmentAddress = this.EntertainmentAddressTxb.Text.Trim();
            string strEntertainmentPhone = this.EntertainmentPhoneTxb.Text.Trim();
            string strEntertainmentType = this.EntertainmentTypeTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strEntertainmentName != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.EntertainmentCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.EntertainmentCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceEntertainmentInfo(new Guid(base.Request.QueryString["EntertainmentId"]), strEntertainmentName, new Guid(this.EntertainmentProvinceDdl.SelectedItem.Value), cityId, strEntertainmentAddress, strEntertainmentPhone, strEntertainmentType, this.EntertainmentRecommendationRateDdl.SelectedItem.Value);
                    base.Response.Redirect("~/Resource/ResourceEntertainmentPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strEntertainmentName != "")
            {
                if (!ManageResource.IsExistSameResourceEntertainmentInfo(strEntertainmentName))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.EntertainmentCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.EntertainmentCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceEntertainmentInfo(strEntertainmentName, new Guid(this.EntertainmentProvinceDdl.SelectedItem.Value), cityId, strEntertainmentAddress, strEntertainmentPhone, strEntertainmentType, this.EntertainmentRecommendationRateDdl.SelectedItem.Value);
                    base.Response.Redirect("~/Resource/ResourceEntertainmentPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbEntertainmentName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbEntertainmentProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentProvinceOverSea", language, path);
                this.lbEntertainmentCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentCityOverSea", language, path);
            }
            else
            {
                this.lbEntertainmentProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentProvince", language, path);
                this.lbEntertainmentCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentCity", language, path);
            }
            this.lbEntertainmentAddress.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentAddress", language, path);
            this.lbEntertainmentPhone.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentPhone", language, path);
            this.lbEntertainmentType.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentType", language, path);
            this.lbEntertainmentRecommendationRate.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "lbEntertainmentRecommendationRate", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEntertainmentPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

